package com.education.business.service.education;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.education.business.service.system.*;
import com.education.common.utils.FileUtil;
import com.education.common.utils.ZipUtils;
import com.education.model.entity.*;
import com.education.model.subproject.ExamTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

@Service
public class UpDownLoadservice {
    @Autowired
    private ExamTaskService examTaskService;
    @Autowired
    private ExamRoomService examRoomService;
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private TestPaperInfoService testPaperInfoService;
    @Autowired
    private QuestionInfoService questionInfoService;
    @Autowired
    private GradeInfoService gradeInfoService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private SubjectInfoService subjectInfoService;
    @Autowired
    private SystemAdminRoleService systemAdminRoleService;
    @Autowired
    private SystemDictService systemDictService;
    @Autowired
    private SystemDictValueService systemDictValueService;
    @Autowired
    private SystemMenuService systemMenuService;
    @Autowired
    private SystemRoleMenuService systemRoleMenuService;
    @Autowired
    private SystemRoleService systemRoleService;
    @Autowired
    private TestPaperQuestionInfoService testPaperQuestionInfoService;
    @Autowired
    private TestPaperInfoStudentService testPaperInfoStudentService;



    /**
     * 考试任务导出
     * 根据考试任务ID读写数据库数据成json数据
     * 将读写后的json数据压缩成压缩包
     * 删除读写json数据后的目录
     * @param examTaskId
     * @return 压缩包的位置（String）
     */
    public String downLoadTaskById(HttpServletResponse response,int examTaskId){
        //创建文件夹
        FileUtil.CreateFile("ExamTask");
        FileUtil.CreateFile("ExamTaskZip");
        ExamTask examTasks = examTaskService.ExamTaskById(examTaskId);
        examRoomService.ExamRoomByTaskId(examTasks.getExamRoomId());
        systemAdminService.selectByTaskId(examTasks.getAdminId());
        systemAdminRoleService.selectByAdminId(examTasks.getAdminId());
        SystemRole systemRole = systemRoleService.findRoleByAdminId(examTasks.getAdminId());
        systemDictService.SystemDict();
        systemDictValueService.SystemDictValue();
        systemRoleMenuService.SystemRoleMenuByRoleId(systemRole.getId());
        List systemRoles = new ArrayList();
        systemRoles.add(systemRole.getId());
        systemMenuService.selectMenuListByRoles(systemRoles);
        List<TestPaperInfo> testPaperInfo = testPaperInfoService.testPaperInfoListByTaskId(examTasks.getId());
        JSONArray testPaperArray = JSON.parseArray(JSON.toJSONString(testPaperInfo));
        List<TestPaperQuestionInfo> testPaperQuestionInfoList = new ArrayList();
        testPaperQuestionInfoList = testPaperQuestionInfoService.TestPaperQuestionInfoByPaperId(testPaperInfo);
        questionInfoService.questionById(testPaperQuestionInfoList);
        testPaperInfoStudentService.TestPaperInfoStudentByTaskId(examTasks.getId());
        studentInfoService.selectStudentByTestPaperId(testPaperArray);
        gradeInfoService.gradeInfoById(testPaperArray);
        subjectInfoService.selectByTestPaperId(testPaperArray);
        //压缩包名字
        String zipName = "考试任务"+"_"+examTasks.getName()+".zip";
        //需要压缩的目录
        Path folder = Paths.get("education-api"+ File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"ExamTask");
        //压缩后的位置
        //相对路径
        String absolutePath = System.getProperty("user.dir");//项目路径
        String FileZip = "education-api"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"static"+File.separator+"ExamTaskZip"+File.separator;
//        String FileZip = "D:"+File.separator+"test"+File.separator;
        Path zipFile = Paths.get(FileZip+zipName);
        //压缩目录
        try {
            ZipUtils.packet(folder,zipFile);
        }catch (IOException e){
            e.printStackTrace();
        }
        //删除目录
        File delfile = folder.toFile();
        FileUtil.deleteDir(delfile);

//        File file = new File(zipFile.toString());
//        System.out.println("失败");
//        if (!file.exists()) return "文件不存在";
//
//        System.out.println();
//        try
//         {
//             InputStream inStream = new FileInputStream(file);
//
//             System.out.println("文件路径"+file.getName());
//             OutputStream os = response.getOutputStream();
//            System.out.println("成功"+response.getOutputStream());
//            response.reset();
//            //下载后的文件名
//            response.setContentType("application/force-download");// 设置强制下载不打开
//            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"UTF-8"));
//            byte[] buff = new byte[1024];
//            int len = 0;
//
//            while ((len = inStream.read(buff)) != -1) {
//                os.write(buff, 0, len);
//                System.out.println("len"+len);
//            }
//            os.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "下载文件异常";
//        }
        return zipFile.toString();
    }






}
